package com.eveinfo.api.character.sheet;

import java.util.LinkedList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import com.eveinfo.api.AbstractApiHandler;
import com.eveinfo.api.ApiException;

public class CharacterSheetHandler extends AbstractApiHandler<ApiSheet> {

	private String augmentatorName;
	private int augmentatorValue;
	private LinkedList<String> where;

	public CharacterSheetHandler() {
		super();
		response = new ApiSheet();
		where = new LinkedList<String>();
	}
	
	@Override
	public ApiSheet getParsedData() throws ApiException {
		if(response == null)
			throw new ApiException(error);
		return response;
	}

	@Override
	public void startElement(String uri, String localName,
			String qName, Attributes attributes) throws SAXException {
		super.startElement(uri, localName, qName, attributes);
		String value = (!localName.equals(""))?localName:qName;
		if (value.equals("row")) {
			String first = where.getFirst();
			if(first.equals("skills")){
				String attrValue;
				ApiKnownSkill skill = new ApiKnownSkill();
				attrValue = attributes.getValue("typeID");
				skill.setTypeID(Integer.parseInt(attrValue));
				attrValue = attributes.getValue("level");
				skill.setLevel(Integer.parseInt(attrValue));
				attrValue = attributes.getValue("skillpoints");
				skill.setSkillpoints(Integer.parseInt(attrValue));
				response.addSkill(skill);
			}else if (first.equals("certificates")) {
				response.addCertificate(Integer.parseInt(attributes.getValue("certificateID")));
			}
		} else if (value.equals("rowset")) {
			where.addFirst(attributes.getValue("name"));
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		super.endElement(uri, localName, qName);
		String value = (!localName.equals(""))?localName:qName;
		if (value.equals("characterID")) {
			response.setCharacterID(Integer.parseInt(text));
		} else if (value.equals("name")) {
			response.setName(text);
		} else if (value.equals("race")) {
			response.setRace(text);
		} else if (value.equals("bloodLine")) {
			response.setBloodLine(text);
		} else if (value.equals("gender")) {
			response.setGender(text);
		} else if (value.equals("corporationName")) {
			response.setCorporationName(text);
		} else if (value.equals("corporationID")) {
			response.setCorporationID(Integer.parseInt(text));
		} else if (value.equals("balance")) {
			response.setBalance(Double.parseDouble(text));
		} else if (value.equals("charismaBonus")) {
			response.addAttributeEnhancer(new CharismaBonus(augmentatorName,
					augmentatorValue));
		} else if (value.equals("intelligenceBonus")) {
			response.addAttributeEnhancer(new IntelligenceBonus(
					augmentatorName, augmentatorValue));
		} else if (value.equals("memoryBonus")) {
			response.addAttributeEnhancer(new MemoryBonus(augmentatorName,
					augmentatorValue));
		} else if (value.equals("perceptionBonus")) {
			response.addAttributeEnhancer(new PerceptionBonus(augmentatorName,
					augmentatorValue));
		} else if (value.equals("willpowerBonus")) {
			response.addAttributeEnhancer(new WillpowerBonus(augmentatorName,
					augmentatorValue));
		} else if (value.equals("augmentatorName")) {
			augmentatorName = new String(text);
		} else if (value.equals("augmentatorValue")) {
			augmentatorValue = Integer.parseInt(text);
		} else if (value.equals("memory")) {
			response.setMemory(Integer.parseInt(text));
		} else if (value.equals("charisma")) {
			response.setCharisma(Integer.parseInt(text));
		} else if (value.equals("perception")) {
			response.setPerception(Integer.parseInt(text));
		} else if (value.equals("willpower")) {
			response.setWillpower(Integer.parseInt(text));
		} else if (value.equals("intelligence")) {
			response.setIntelligence(Integer.parseInt(text));
		} else if (value.equals("rowset")) {
			where.removeFirst();
		}
	}
}
